<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 获取配置
 */
namespace app\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\allwin\model\AllwinConfig;
use app\allwin\model\Vip;
use app\allwin\model\AllwinInfoMp;
use app\allwin\model\StoreWorker;
use app\allwin\model\Levels;

class Config extends Base{

     /**
     * 获取应用配置
     **/
    public function index(){
        $info = AllwinConfig::getConfig($this->miniapp->id);
        if(!isset($info)) {
            return enjson(403,'应用未配置');
        }
        $article = json_decode($info->article);
        $config['service_telephone']  = $info->service_telephone ??'';
        $config['shore_img']          = $article->shore_img ??'';
        $config['shore_text']         = $article->shore_text ??'';
        $config['search_tip']         = $article->search_tip ??'';
        $config['index_more']         = $article->index_more ??'';
        $config['index_shop_title']   = $article->index_shop_title ??'';
        //会员页面文字
        $config['vip_front_text']     = $article->vip_front_text ??'';
        $config['vip_behind_text']    = $article->vip_behind_text ??'';
        //优选分享文案
        $config['shop_share_text']    = $article->shop_share_text ??'';
        $config['shop_share_img']     = $article->shop_share_img ??'';
        //好店分享文案
        $config['store_share_text']   = $article->store_share_text ??'';
        $config['store_share_img']    = $article->store_share_img ??'';
        //判断是否会员
        $config['user_card']          = ['title' =>$article->user_card->title,'page'  =>$article->user_card->page,'type' => $article->user_card->type];
        $config['is_vip']             = 0;
        $config['is_store_manage']    = 0;
        //判断是否城市号
        $config['is_info_vip']        = 0;
        if($this->user){
            $is_vip = Vip::where(['user_id' => $this->user->id,'state' => 1,'is_lock' => 0])->field('vipcard_id,id')->find();
            if($is_vip){
                $config['is_vip']    = 1;
                $config['vip_name']  = $is_vip->vipcard->name;
                $config['user_card'] = ['page' =>'/pages/money/index','type' => 'navigate'];
            }
            $is_info_vip = AllwinInfoMp::where(['uid' => $this->user->id,'is_vip' => 1,'is_lock' => 0])->count();
            if($is_info_vip){
                $config['is_info_vip'] = 1;
                $config['info_vip']    = '蓝V认证';
            }
            $is_store_manage  = StoreWorker::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id,'is_cashier' => 1])->find();
            if($is_store_manage){
                if($is_store_manage->store->is_lock == 0){
                    $config['is_store_manage'] = 1;
                } 
            }
        }
        return enjson(200,'应用配置',$config);
    }

    /**
     * 读取我的推荐用户
     * @return void
     */
    public function levelUser(){
        $param['page']    = $this->request->param('page/d');
        $param['signkey'] = $this->request->param('signkey');
        $param['sign']    = $this->request->param('sign');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        if(!$this->user) {
            return enjson(204);
        }
        $info = Levels::levelUser($this->user->id,[1,2]);
        if($info){
            $num = Levels::where(['parent_id' => $this->user->id,'level' => [1,2]])->count();
            return enjson(200,'成功',['list'=>$info,'num' => $num]);
        }
        return enjson(204);
    }
}